﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Workflow.Model.Enum;

namespace Workflow.Web.Common
{
    public class UserAuthorizeAttribute : AuthorizeAttribute//? FilterAttribute,IAuthorizationFilter
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {

            //Uri urlReferrer = filterContext.HttpContext.Request.UrlReferrer;
            //if (urlReferrer != null)
            //{
                //string domain = urlReferrer.Host.ToLower();
                //string hostStr = HttpContext.Current.Request.Url.Host.ToLower();

                //if (domain != hostStr)
                //    filterContext.Result = new RedirectResult("/Error/NotReferrer");


                if (filterContext.HttpContext.Session != null)
                {


                    if (filterContext.HttpContext.Session[ConfigReader.AuthorizeSessionUserName] == null)
                    {
                        if (filterContext.HttpContext.Request.Url != null)
                            filterContext.HttpContext.Session["url"] = filterContext.HttpContext.Request.Url.ToString();
                        filterContext.Result = new RedirectResult("/Account/LogOn");
                    }



                    var controller = filterContext.RouteData.Values["controller"].ToString().ToLower();
                    var action = filterContext.RouteData.Values["action"].ToString().ToLower();



                    //var isAllowed = this.IsAllowed(user, controller, action); 
                }
            //}
            //else
            //{
            //    filterContext.Result = new RedirectResult("/Error/NotReferrer");
            //}
        }

        //bool IsAllowed(User user, string controllerName, string actionName)  
        //{ 
        //    var service = ServiceLoader.LoadService<ToySpirit.IToySpiritService.IControllerActionService>(); 
        //    // 获取对应的controller  
        //    var controller = service.GetSingleByExpression(c => c.Name == controllerName && c.IsController); 
        //    if (controller != null)  
        //    { 
        //        // 获取对应的action 
        //        var controllerAction = service.GetSingleByFunc(c => c.Name == actionName && c.IsController == false && c.ControllerName == controllerName); 
        //        return controllerAction == null ? this.isAllowed(user, controller) : this.isAllowed(user, controllerAction); 
        //    }
        //    // 没有定义controller的权限，表示无需权限控制 
        //    return true; 
        //}
    }
}